{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 双通道跨模态上下文交互模块 \n",
    "\n",
    "> 该模块出自于:[Hierarchical denoising representation disentanglement and dual-channel cross-modal-context interaction for multimodal sentiment analysis](zotero://select/library/items/4RUW8L58)\n",
    "\n",
    "**PyTorch Code**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch.nn.functional as F\n",
    "\n",
    "class CrossModalInteraction(nn.Module):\n",
    "    def __init__(self, dim):\n",
    "        super(CrossModalInteraction, self).__init__()\n",
    "        self.self_attention = nn.MultiheadAttention(dim, num_heads=4)\n",
    "        self.cross_attention = nn.MultiheadAttention(dim, num_heads=4)\n",
    "\n",
    "    def forward(self, modality_1, modality_2):\n",
    "        # Self-attention for intra-modal interactions\n",
    "        intra_modality_1, _ = self.self_attention(modality_1, modality_1, modality_1)\n",
    "        intra_modality_2, _ = self.self_attention(modality_2, modality_2, modality_2)\n",
    "\n",
    "        # Cross-attention for inter-modal interactions\n",
    "        inter_modality_1, _ = self.cross_attention(modality_1, modality_2, modality_2)\n",
    "        inter_modality_2, _ = self.cross_attention(modality_2, modality_1, modality_1)\n",
    "\n",
    "        return intra_modality_1 + inter_modality_1, intra_modality_2 + inter_modality_2"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
